package com.wwg.market.ui.category.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.mobileares.midp.widgets.client.button.ButtonBar;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.list.ListItem;
import com.wwfish.gwt.model.client.IFishModel;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwfish.gwt.model.client.impl.ListModel;
import com.wwfish.gwt.model.client.impl.Listener;
import com.wwfish.gwt.model.client.impl.ListenerHandler;
import com.wwg.market.ui.category.client.widgets.CategoryForm;
import com.wwg.market.ui.category.client.widgets.CategoryWidget;
import com.wwg.market.ui.dashboard.client.widgets.MyPage;
import com.wwg.market.ui.dashboard.client.widgets.PageItem;
import com.wwg.market.ui.service.dto.CategoryDto;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-5
 * Time: 18:04:52
 * To change this template use File | Settings | File Templates.
 */
public class CategoryOperPageItem extends PageItem {
    private CategoryWidget categoryWidget;
    private CategoryWidget categoryWidget2;
    private CategoryWidget categoryWidget3;
    private CButton create1;
    private CategoryForm popForm;


    @Override
    protected void processMain(SimplePanel mainPanel) {
        categoryWidget = new CategoryWidget(this);
        categoryWidget2 = new CategoryWidget(this);
        categoryWidget3 = new CategoryWidget(this, false);
        popForm = new CategoryForm();

        HorizontalPanel categories = new HorizontalPanel();
        categories.setSpacing(1);
        categories.add(categoryWidget);
        categories.add(categoryWidget2);
        categories.add(categoryWidget3);

        mainPanel.setWidget(categories);

        categoryWidget.setCreateChildCommand(new Command() {
            public void execute() {
                saveChildCategory(categoryWidget, categoryWidget2, "创建二级类别");
            }
        });
        categoryWidget2.setCreateChildCommand(new Command() {
            public void execute() {
                saveChildCategory(categoryWidget2, categoryWidget3, "创建三级类别");
            }
        });
        categoryWidget.setSelectionHandler(new SelectionHandler<ListItem>() {
            public void onSelection(SelectionEvent<ListItem> listItemSelectionEvent) {
                CategoryDto parent = (CategoryDto) listItemSelectionEvent.getSelectedItem().getUserObject();
                categoryWidget2.getCategoryListWidget().getFishModel().load(parent.getChildren());
                categoryWidget2.setParentCategory(parent);
            }
        });
        categoryWidget2.setSelectionHandler(new SelectionHandler<ListItem>() {
            public void onSelection(SelectionEvent<ListItem> listItemSelectionEvent) {
                CategoryDto parent = (CategoryDto) listItemSelectionEvent.getSelectedItem().getUserObject();
                categoryWidget3.getCategoryListWidget().getFishModel().load(parent.getChildren());
                categoryWidget3.setParentCategory(parent);
            }
        });
    }

    @Override
    protected void processMenu(ButtonBar bar) {
        create1 = new CButton("创建类别");
        bar.addButton(create1);

        create1.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                popForm.setTitle("创建根类别");
                popForm.setModel(categoryWidget.getCategoryListWidget().getFishModel());
                popForm.show(null, new CategoryDto());
            }
        });
    }

    public CategoryForm getForm() {
        return popForm;
    }


    @Override
    public void initModel() {
        categoryWidget.getCategoryListWidget().setFishModel((ListModel<CategoryDto>) FishModelManager.getAppModel("channels"));
        categoryWidget2.getCategoryListWidget().setFishModel(getTempListModel());
        categoryWidget3.getCategoryListWidget().setFishModel(getTempListModel());

        categoryWidget.getCategoryListWidget().getFishModel().getListenerManager().addListener(Listener._LOAD,
                new ListenerHandler() {
                    public void execute(IFishModel model) {
                        categoryWidget2.getCategoryListWidget().getFishModel().load(null);
                        categoryWidget2.setParentCategory(null);
                    }
                });

        categoryWidget2.getCategoryListWidget().getFishModel().getListenerManager().addListener(Listener._LOAD,
                new ListenerHandler() {
                    public void execute(IFishModel model) {
                        categoryWidget3.getCategoryListWidget().getFishModel().load(null);
                        categoryWidget3.setParentCategory(null);
                    }
                });
    }

    public void saveChildCategory(CategoryWidget parentWidget, CategoryWidget currentWidget, String title) {
        CategoryDto parent = (CategoryDto) parentWidget.getCategoryListWidget().getBMUI().getSelected().getUserObject();
        popForm.setTitle(title);
        popForm.setModel(currentWidget.getCategoryListWidget().getFishModel());
        CategoryDto dto = new CategoryDto();
        popForm.show(parent, dto);
    }

    public ListModel<CategoryDto> getTempListModel() {
        ListModel<CategoryDto> model = (ListModel<CategoryDto>) FishModelManager.createTempModel(FishModelManager.TYPE_LIST);
        return model;
    }

    @Override
    public void ddOutModel(Object ob) {
        categoryWidget.render();
    }
}
